package com.rookie.tool.module.example.excel;

import com.rookie.tool.module.util.excel.ExcelProcessUtils;
import com.rookie.tool.module.util.excel.SelectConfig;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

@Component
public class TestRun {

    @Autowired
    private TestExcelDataHandle testExcelDataHandle;

    /**
     * 文件读取
     */
    public void readData() {
        try {
            ExcelProcessUtils.readAllSheet(new FileInputStream("E:\\personal-project\\java\\spring-test\\test.xlsx"), Person.class, testExcelDataHandle);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件写入
     */
    public void writeData() {
        List<Person> peopleList = new LinkedList<>();
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            Person person = new Person();
            person.setAddress(RandomStringUtils.randomAlphanumeric(20));
            person.setAge(random.nextInt(1000));
            person.setName(RandomStringUtils.randomAlphanumeric(4));
            peopleList.add(person);
        }
        ExcelProcessUtils.writeSheet("D:\\tt.xlsx", Person.class, peopleList);
        System.out.println("=====================");
    }

    public void writeDataHavePullBox() {
        List<Person> peopleList = new LinkedList<>();
        Person t1 = new Person();
        t1.setAge(5);
        peopleList.add(t1);
        Person t2 = new Person();
        t2.setAge(3);
        peopleList.add(t2);
        List<SelectConfig> list = new LinkedList<>();
        list.add(new SelectConfig()
                .setStartX(0)
                .setStartY(0)
                .setEndX(0)
                .setEndY(4)
                .setList(new String[]{"测试5", "测试2"}));
        ExcelProcessUtils.writeSheet("D:\\tt.xlsx", Person.class, peopleList, list);
    }
}
